import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/5/14 0014.
 */

public class Card extends FrameLayout {
    //2048卡片的文字
    private TextView label;
    private int num=0;//用来判断卡片数字
    public Card(Context context){
        super(context);
        //初始化文本;
        label = new TextView(getContext());
        //设置文本的大小
        label.setTextSize(32);
        //设置Card的背景颜色
        //label.setBackgroundColor(0xFFC4E4FF);
        //把放在控件里的文本居中处理
        label.setGravity(Gravity.CENTER);

        //布局参数用来控制
        LayoutParams lp=new LayoutParams(-1,-1);//用来初始化layout控件TextView里的高和宽属性
        lp.setMargins(15,15,0,0);//给每个TextView的左和上设置margin
        addView(label,lp);//该方法是用来给label控件加上已经初始化的高和宽
        setNum(0);
    }

    public int getNum(){
        return num;
    }
    public void setNum(int num){
        this.num=num;

        //导入随机数字，排除出现0
        if(num<=0){
            label.setText("");
            this.DrawCard(0);
        }else{
            label.setText(num+"");
            this.DrawCard(num);
        }
        //呈现数字
        //label.setText(num+"");
    }

    //判断两张卡片数字是否一样
    public boolean equals(Card o){
        return getNum()==o.getNum();
    }

    //判断不同的数上不同的色
    public void DrawCard(int num){
        this.num=num;
        switch (num){
            case 0:
                label.setBackgroundColor(Color.parseColor("#CCC0B3"));
                break;
            case 2:
                label.setBackgroundColor(Color.parseColor("#EEE4DA"));
                break;
            case 4:
                label.setBackgroundColor(Color.parseColor("#EDE0C8"));
                break;
            case 8:
                label.setBackgroundColor(Color.parseColor("#F2B179"));
                break;
            case 16:
                label.setBackgroundColor(Color.parseColor("#F49563"));
                break;
            case 32:
                label.setBackgroundColor(Color.parseColor("#F5794D"));
                break;
            case 64:
                label.setBackgroundColor(Color.parseColor("#F55D37"));
                break;
            case 128:
                label.setBackgroundColor(Color.parseColor("#EEE863"));
                break;
            case 256:
                label.setBackgroundColor(Color.parseColor("#EDB04D"));
                break;
            case 512:
                label.setBackgroundColor(Color.parseColor("#ECB04D"));
                break;
            case 1024:
                label.setBackgroundColor(Color.parseColor("#EB9437"));
                break;
            case 2048:
                label.setBackgroundColor(Color.parseColor("#EA7821"));
                break;
            default:
                label.setBackgroundColor(Color.parseColor("#CCC0B3"));
                break;
        }
    }

}